package com.example.onlinemonitoring.serviceImpl;

import com.example.onlinemonitoring.model.Newblqsensor;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Newblqhistory;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

import com.example.onlinemonitoring.Util.tem.SensorAbstractBaseService;
import com.example.onlinemonitoring.dao.NewblqhistoryMapper;
import com.example.onlinemonitoring.dao.NewblqsensorMapper;
import com.example.onlinemonitoring.service.NewblqsensorService;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.ptconfig;
import com.example.onlinemonitoring.model.common.wsdconfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @description Newblqsensor接口实现
 * @author generator
 * @since 2022-04-09 13:03:09
 */
@Service("newblqServiceImpl")
public class NewblqsensorServiceImpl extends SensorAbstractBaseService<Newblqsensor, Newblqhistory>
        implements NewblqsensorService {

    @Autowired
    private NewblqsensorMapper newblqsensormapper;
    @Autowired
    private NewblqhistoryMapper newblqhistorymapper;

    public void UpdateStatus(Appdevice ad, Integer addrindex, int i, String string) {
        Newblqsensor record = new Newblqsensor();
        record.setStationid(ad.getStationid());
        record.setDevicegroup(ad.getDevicegroup());
        switch (string) {
            case "blq":
                record.setLastcommunicationstatusBlq(i);
                break;
            case "pt":
                record.setLastcommunicationstatusPt(i);
                break;
            case "wsd":
                record.setLastcommunicationstatusWsd(i);
                break;
            default:
                break;
        }
        record.setDeviceaddr(addrindex);
        Update(record, false);
    }

    public List<Newblqhistory> getRecent6Data(Newblqhistory pthis) {
        Example example = new Example(Newblqhistory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", pthis.getStationid());
        criteria.andEqualTo("devicegroup", pthis.getDevicegroup());
        criteria.andEqualTo("deviceaddr", pthis.getDeviceaddr());
        criteria.andEqualTo("ptaddr", pthis.getPtaddr());
        example.orderBy("datatime").desc();
        RowBounds rb = new RowBounds(0, 6);
        List<Newblqhistory> blqlist = newblqhistorymapper.selectByExampleAndRowBounds(example, rb);
        return blqlist;
    }

    public MyResult<wsdconfig> SelectWsd(Newblqsensor record) {
        MyResult<wsdconfig> result = new MyResult<wsdconfig>();
        List<wsdconfig> list = new ArrayList<wsdconfig>();
        wsdconfig selectwsd = newblqsensormapper.selectwsd(record);
        list.add(selectwsd);
        if (selectwsd != null) {
            result.setCode(0);
            result.setMsg("success");
            result.setCount(list != null && !list.isEmpty() ? list.size() : 0);
            result.setData(selectwsd != null ? list : null);
        } else {
            result.setCode(1);
            result.setMsg("数据为空");
        }
        return result;
    }

    public MyResult<ptconfig> SelectPt(Newblqsensor record) {
        MyResult<ptconfig> result = new MyResult<ptconfig>();
        List<ptconfig> list = newblqsensormapper.selectpt(record);
        if (list.size() != 0) {
            result.setCode(0);
            result.setMsg("success");
            result.setCount(list != null && !list.isEmpty() ? list.size() : 0);
            result.setData(list);
        } else {
            result.setCode(1);
            result.setMsg("数据为空");
        }
        return result;
    }

    @Transactional
    public MyResult<Integer> UpdateWsd(wsdconfig wsdconfig, Integer stationid, Integer devicegroup) {
        Example example = new Example(Newblqsensor.class);
        Example.Criteria criteria = example.createCriteria();
        Newblqsensor blq = new Newblqsensor();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        List<Newblqsensor> blqlist = newblqsensormapper.selectByExample(example);
        if (wsdconfig.getBlqmax() == null || wsdconfig.getBlqmin() == null || wsdconfig.getWsdmax() == null
                || wsdconfig.getWsdmin() == null) {
            for (int i = 0; i < blqlist.size(); i++) {
                blq.setId(blqlist.get(i).getId());
                newblqsensormapper.setWsdaddr(null, blqlist.get(i).getId());
            }
        } else {
            HashMap<Integer, Integer> currblqmap = new HashMap<Integer, Integer>();
            for (int j = wsdconfig.getBlqmin(); j <= wsdconfig.getBlqmax(); j++) {
                currblqmap.put(j, j - wsdconfig.getBlqmin() + wsdconfig.getWsdmin());
            }
            for (int i = 0; i < blqlist.size(); i++) {
                newblqsensormapper.setWsdaddr(currblqmap.get(blqlist.get(i).getDeviceaddr()), blqlist.get(i).getId());
            }
        }
        MyResult<Integer> m = new MyResult<Integer>();
        m.setCode(MyResult.SUCCESS);
        m.setMsg("关联成功！");
        return m;
    }

    @Transactional
    public MyResult<Integer> UpdatePt(ptconfig ptconfig, Integer stationid, Integer devicegroup) {
        Example example = new Example(Newblqsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        newblqsensormapper.setPtaddr(stationid, devicegroup, ptconfig.getPtaddr());
        if (ptconfig.getBlqmin() != 0 && ptconfig.getBlqmax() != 0) {
            Newblqsensor record = new Newblqsensor();
            record.setPtaddr(ptconfig.getPtaddr());
            criteria.andGreaterThanOrEqualTo("deviceaddr", ptconfig.getBlqmin());
            criteria.andLessThanOrEqualTo("deviceaddr", ptconfig.getBlqmax());
            updateByExampleSelective(record, example);
        }
        return new MyResult<Integer>(true);
    }

    public List<Newblqsensor> getPtModel(Newblqsensor alarm) {
        Example example = new Example(Newblqsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", alarm.getStationid());
        criteria.andEqualTo("devicegroup", alarm.getDevicegroup());
        criteria.andEqualTo("ptaddr", alarm.getPtaddr());
        example.orderBy("deviceaddr").asc();
        return newblqsensormapper.selectByExample(example);
    }

    public List<Newblqsensor> getWsdModel(Newblqsensor alarm) {
        Example example = new Example(Newblqsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", alarm.getStationid());
        criteria.andEqualTo("devicegroup", alarm.getDevicegroup());
        criteria.andEqualTo("wsdaddr", alarm.getWsdaddr());
        return newblqsensormapper.selectByExample(example);
    }
}
